473,418 Members | 3,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Button Routines being called twice

I am sorry but I am all very new and slow at understanding all this
ASP.NET2.

I found some code which showed how to page with a repeater. All very
excited as I had been looking for this all day. It worked wonderfully,
but it was in C# So I converted it to VB with

http://www.developerfusion.co.uk/uti...sharptovb.aspx

It got upset about private and so I changed those to protected

My problem is the NEXT Button subroutine gets called twice, but the
PREV one only got called once.

After lots of hair pulling, I eventually worked out that it was because
the PREV button did not have the Handles cmdPrev.Click. I added that
to the PREV Button and now that executes twice. I do not fully
understand this Handles reference, but it is always there if I set up a
default event for a Button.

I don't think it is a postback problem, because the page is only
loading once and I have commented out all other code.

OK so why is / are the Button routines being executed twice.

The code is as follows.

Imports System.data
Partial Class timrepeat
Inherits System.Web.UI.Page
Public Property CurrentPage() As Integer
Get
Dim cpg As Object = Me.ViewState("_CurrentPage")
If cpg Is Nothing Then
Return 0
Else
Return CType(cpg, Integer)
End If
End Get
Set(ByVal value As Integer)
Me.ViewState("_CurrentPage") = value
End Set
End Property

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
'ItemsGet()
lbxDebug.Items.Insert(0, "PageLoad:
--->>>>>>>>>>>>>>>>>>>>>>>>>--- Curr Page:" & CurrentPage.ToString)

End Sub

Private Sub ItemsGet()
Dim Items As DataSet = New DataSet
Items.ReadXml(MapPath("Items.xml"))
Dim objPds As PagedDataSource = New PagedDataSource
objPds.DataSource = Items.Tables(0).DefaultView
objPds.AllowPaging = True
objPds.PageSize = 1
objPds.CurrentPageIndex = CurrentPage
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString + "
of " + objPds.PageCount.ToString
cmdPrev.Enabled = Not objPds.IsFirstPage
cmdNext.Enabled = Not objPds.IsLastPage
repeaterItems.DataSource = objPds
repeaterItems.DataBind()
End Sub

Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdNext.Click
lbxDebug.Items.Insert(0, "NEXTBEFORE:CurrentPage= " &
CurrentPage.ToString)
CurrentPage += 1
lbxDebug.Items.Insert(0, "NEXTAFTER:CurrentPage= " &
CurrentPage.ToString)
'ItemsGet()
End Sub

Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdPrev.Click
lbxDebug.Items.Insert(0, "PREVBEFORE:CurrentPage= " &
CurrentPage.ToString)
CurrentPage -= 1
lbxDebug.Items.Insert(0, "PREVAFTER:CurrentPage= " &
CurrentPage.ToString)
'ItemsGet()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

End Sub
End Class

The aspx page is

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="timrepeat.aspx.vb" Inherits="timrepeat" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%--<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>
--%>
<html>
<head>
<title>TestRepeater</title>
</head>
<body>
<form id="frmTest" method="post" runat="server">
<table width="100%" border="0">
<tr>
<td>&nbsp;&nbsp;Repeater control with Paging functionality</td>
</tr>
<tr>
<td>&nbsp;&nbsp;<asp:label id="lblCurrentPage"
runat="server"></asp:label></td>
</tr>
<tr>
<td>&nbsp;&nbsp;<asp:button id="cmdPrev" runat="server"
text="PREV" onclick="cmdPrev_Click"></asp:button>
&nbsp;<asp:button id="cmdNext" runat="server" text="NEXT"
onclick="cmdNext_Click"></asp:button></td>
</tr>
</table>
<table border="1">
<asp:repeater id="repeaterItems" runat="server">
<itemtemplate>
<tr>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemName") %></b></td>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemDescription") %></b></td>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemPrice") %></b></td>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemInStock") %></b></td>
</tr>
</itemtemplate>
</asp:repeater>
</table>
&nbsp;
<asp:ListBox ID="lbxDebug" runat="server"
Rows="10"></asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>

May 24 '06 #1
9 4717
I had a similar problem with .net 1.1 and c#.
Figured out that AutoEventWireup s/be false. I am not sure how this
will be in 2.0 and vb.
Hope this helps.

May 24 '06 #2
I can not see where I could try autoeventwireup. The button does not
seem to have that property.

May 24 '06 #3
This is on the Page (.aspx) and not button, usually the 1st line
Here is my code snippet:
<%@ Page language="c#" Codebehind="searchResults.aspx.cs"
AutoEventWireup="false" Inherits="searchResults" %>

May 24 '06 #4
It just shows my lack of knowledge and understanding - I already had
that in my original page declaration, see original post.

Thanks for your input, but it looks like back to the drawing board for
a solution.

May 24 '06 #5
Yes, I missed this.
In my case, I inserted a record into a table on postBack with
date-time stamp, control name just to figure out what is happening.

May 24 '06 #6
There is some underlying events/logic, which I am not understanding, as
I said originally, if I remove the handles element from the Button
Routines they work. Now I can not grasp what the 'Handles
button1.click' do/mean.

If anyone can enlighten me - I would be most grateful.

May 24 '06 #7
"Handles" is VB.NET syntax for "Make this handle the XXX_whatever event"
(Click, etc.)
It does the same thing that AutoEventWireup does at the page level, hence
you could have your events firing twice for the control.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"mosscliffe" wrote:
There is some underlying events/logic, which I am not understanding, as
I said originally, if I remove the handles element from the Button
Routines they work. Now I can not grasp what the 'Handles
button1.click' do/mean.

If anyone can enlighten me - I would be most grateful.

May 25 '06 #8
Thank you. I will get there - eventually - he says - through clenched
teeth and little conviction !

May 25 '06 #9
The handles makes VB make a call to AddHandler which registers the a
delegate for the methods on the click event for the button. This will make
the button click call once.
In the aspx page however, you have the OnClick attributes of the buttons
pointing to the methods, this will also make a call to addhandler so you
will have 2 delegates for the same method on the same event. Therefore it
will run twice.
Remove one or the other, you dont need both,
HTH

Ciaran

"mosscliffe" <pa***********@googlemail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
I am sorry but I am all very new and slow at understanding all this
ASP.NET2.

I found some code which showed how to page with a repeater. All very
excited as I had been looking for this all day. It worked wonderfully,
but it was in C# So I converted it to VB with

http://www.developerfusion.co.uk/uti...sharptovb.aspx

It got upset about private and so I changed those to protected

My problem is the NEXT Button subroutine gets called twice, but the
PREV one only got called once.

After lots of hair pulling, I eventually worked out that it was because
the PREV button did not have the Handles cmdPrev.Click. I added that
to the PREV Button and now that executes twice. I do not fully
understand this Handles reference, but it is always there if I set up a
default event for a Button.

I don't think it is a postback problem, because the page is only
loading once and I have commented out all other code.

OK so why is / are the Button routines being executed twice.

The code is as follows.

Imports System.data
Partial Class timrepeat
Inherits System.Web.UI.Page
Public Property CurrentPage() As Integer
Get
Dim cpg As Object = Me.ViewState("_CurrentPage")
If cpg Is Nothing Then
Return 0
Else
Return CType(cpg, Integer)
End If
End Get
Set(ByVal value As Integer)
Me.ViewState("_CurrentPage") = value
End Set
End Property

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
'ItemsGet()
lbxDebug.Items.Insert(0, "PageLoad:
--->>>>>>>>>>>>>>>>>>>>>>>>>--- Curr Page:" & CurrentPage.ToString)

End Sub

Private Sub ItemsGet()
Dim Items As DataSet = New DataSet
Items.ReadXml(MapPath("Items.xml"))
Dim objPds As PagedDataSource = New PagedDataSource
objPds.DataSource = Items.Tables(0).DefaultView
objPds.AllowPaging = True
objPds.PageSize = 1
objPds.CurrentPageIndex = CurrentPage
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString + "
of " + objPds.PageCount.ToString
cmdPrev.Enabled = Not objPds.IsFirstPage
cmdNext.Enabled = Not objPds.IsLastPage
repeaterItems.DataSource = objPds
repeaterItems.DataBind()
End Sub

Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdNext.Click
lbxDebug.Items.Insert(0, "NEXTBEFORE:CurrentPage= " &
CurrentPage.ToString)
CurrentPage += 1
lbxDebug.Items.Insert(0, "NEXTAFTER:CurrentPage= " &
CurrentPage.ToString)
'ItemsGet()
End Sub

Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdPrev.Click
lbxDebug.Items.Insert(0, "PREVBEFORE:CurrentPage= " &
CurrentPage.ToString)
CurrentPage -= 1
lbxDebug.Items.Insert(0, "PREVAFTER:CurrentPage= " &
CurrentPage.ToString)
'ItemsGet()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

End Sub
End Class

The aspx page is

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="timrepeat.aspx.vb" Inherits="timrepeat" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%--<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>
--%>
<html>
<head>
<title>TestRepeater</title>
</head>
<body>
<form id="frmTest" method="post" runat="server">
<table width="100%" border="0">
<tr>
<td>&nbsp;&nbsp;Repeater control with Paging functionality</td>
</tr>
<tr>
<td>&nbsp;&nbsp;<asp:label id="lblCurrentPage"
runat="server"></asp:label></td>
</tr>
<tr>
<td>&nbsp;&nbsp;<asp:button id="cmdPrev" runat="server"
text="PREV" onclick="cmdPrev_Click"></asp:button>
&nbsp;<asp:button id="cmdNext" runat="server" text="NEXT"
onclick="cmdNext_Click"></asp:button></td>
</tr>
</table>
<table border="1">
<asp:repeater id="repeaterItems" runat="server">
<itemtemplate>
<tr>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemName") %></b></td>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemDescription") %></b></td>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemPrice") %></b></td>
<td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
"ItemInStock") %></b></td>
</tr>
</itemtemplate>
</asp:repeater>
</table>
&nbsp;
<asp:ListBox ID="lbxDebug" runat="server"
Rows="10"></asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>

Jun 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Logger | last post by:
Help, I’m trying to implement a confirm button on an asp.net page. I have it attached to a asp:button control. In the button1 click event I call the CreateConfirmBox subroutine. The Box comes...
3
by: Jason Kyle Baginski | last post by:
Here's a little test app to demonstrate a problem I'm having. It creates four buttons, each one with the different FlatStyle types available. Three of them behave exactly the same way(and the...
5
by: Nikhil Patel | last post by:
Hi all, I have a strange problem. I have a button called btnSubmit on a .aspx page. I have written btnSubmit_Click method in C# to handle the button's click event. In the .aspx file, the onclick...
1
by: mike parr | last post by:
I have an button which calls its Click procedure twice when clicked. This is my code : public void btnAddToCartPersonal_Click(object sender, System.Web.UI.ImageClickEventArgs e) { //validate...
0
by: Oz | last post by:
Hi Using VS.NET 2003, Windows XP SP1, We have a page which has been developed using ASP.NET. On it, is a button which when clicked is supposed to add some data to a table. When the button is...
2
by: Samy | last post by:
Hi There, I have a user control with buttons on it which I use on a aspx page (parent page). On a button click, a modal dialog(aspx page) opens up and the user enters some info in the modal dialog...
3
by: Imran Aziz | last post by:
Hello All, I have a search text and button that post data and my button handler filters the repeater control. However when the button is clicked the first time. The page_load event is being called...
0
by: Diffident | last post by:
Hello All, I have an asp:button and I have tied an eventhandler to this button's click event which means that the eventhandler should be executed everytime I click the button. But to my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.